﻿using System;

namespace MediaAutomatorLib.ExternalPrograms
{
    public class TheRenamerProgram : ExternalProgram
    {        
        private const string FetchTvShowsArgsTemplate = "-fetch -ff={0} -af={1}";
        private const string FetchMoviesArgsTemplate = @"-fetchmovie -ff={0} -af={1}";

        private readonly string _command;
        private readonly string _targetMoviesFolder;
        private readonly string _targetTvShowsFolder;

        private readonly string _moviesTemporary;
        private readonly string _tvShowsTemporary;
        public TheRenamerProgram(string theRenamerPath, string targetMoviesFolder, string targetTvShowsFolder, string moviesTemporary, string tvShowsTemporary)
        {
            _targetMoviesFolder = FormatPath(targetMoviesFolder);
            _targetTvShowsFolder = targetTvShowsFolder;

            _moviesTemporary = moviesTemporary;
            _tvShowsTemporary = tvShowsTemporary;

            _command = theRenamerPath;
        }

        private String FormatPath(string path)
        {
            if(!path.EndsWith("\\"))
                return path;
            int index = path.LastIndexOf("\\");
            path = path.Substring(0, index);            
            return path;
        }

        public void FetchMovies()
        {
            string args = String.Format(FetchMoviesArgsTemplate,
                                        "\"" + _moviesTemporary + "\"",                                        
                                        "\"" + _targetMoviesFolder + "\"");
            Console.WriteLine("{0} {1}", _command, args);
            ExecuteCommandSync(_command, args);
            
        }

        public void FetchTvShows()
        {
            string args = String.Format(FetchTvShowsArgsTemplate,
                                        "\"" + _tvShowsTemporary + "\"",
                                        "\"" + _targetTvShowsFolder + "\"");
            Console.WriteLine("{0} {1}", _command, args);
            ExecuteCommandSync(_command, args);
            
        }
    }
}
